summaryrefslogtreecommitdiff
path: root/src/options.cpp
blob: 3fff4dcf8ae3ca1b8c45fdf039329f15fc0150df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "options.h"
#include "version.h"
#include "game.h"

#include "smlnode.h"

#include <stdlib.h>

#include <bu/file.h>
#include <bu/optparser.h>
#include <bu/sio.h>
#include <gats/gatsstream.h>
#include <gats/types.h>
#include "datafiles.h"

#include <bu/deflate.h>

#include "interfaceplugger.h"

using namespace Bu;

Options::Options() :
	sInterface("console")
{
}

Options::~Options()
{
}

void Options::parse( int argc, char *argv[] )
{
	Bu::OptParser opt;

	opt.addHelpBanner("STAGE v" VERSION
			" - Simple, Textual, Adventure Game Environment");
	opt.addHelpBanner("usage: " + Bu::String(argv[0]) +
			" [options] <filename>\n");

	Bu::String sIFaces;
	Bu::StringList lIFaces = InterfacePlugger::getInstance().getPluginList();
	bool bBegin = true;
	for( Bu::List<Bu::String>::const_iterator i = lIFaces.begin(); i; i++ )
	{
		if( bBegin )
			bBegin = false;
		else
			sIFaces += ", ";
		sIFaces += *i;
	}

	opt.addOption( Bu::slot( this, &Options::smlTest ), "sml-test",
			"Test SML parser." );
	opt.addOption( Bu::slot( this, &Options::smlHelp ), "sml-help",
			"Display SML help." );
	opt.addOption( sCommand, "command",
			"Set the command to execute once the game is loaded.  "
			"This does not work with all interfaces.");
	opt.addOption( Bu::slot( this, &Options::version ), "version",
			"Show full version info." );
	opt.addOption( Bu::slot( this, &Options::builtins ), "builtins",
			"List available builtins." );
	opt.addOption( Bu::slot( this, &Options::printResult ), "print-result",
			"Print the result from a save file." );
	opt.addOption( sInterface, 'i', "interface",
			"Select interface module. Default is " + sInterface + 
			".  Available modules: " + sIFaces );

	opt.addHelpOption('h', "help");
	opt.setNonOption( Bu::slot( this, &Options::nonOption ) );
	opt.parse( argc, argv );
}

int Options::version( Bu::StrArray aArgs )
{
	sio << "STAGE v" VERSION " - Simple, Textual, Adventure Game Environment."
		<< sio.nl << sio.nl;
	sio << "Full version: " FULLVER << sio.nl;
	sio << "Build date:   " TIMEVER << sio.nl;
	sio << "Build id:     " SHAVER << sio.nl;
	sio << sio.nl;
	exit( 0 );
	return 0;
}

int Options::builtins( Bu::StrArray aArgs )
{
	sio << "Current builtin functions:" << sio.nl;
	Game g;
	const Game::FunctionHash &hFnc = g.getFunctionHash();
	for( Game::FunctionHash::const_iterator i = hFnc.begin(); i; i++ )
	{
		sio << "  - " << i.getKey() << sio.nl;
	}
	sio << sio.nl;
	exit( 0 );
	return 0;
}

int Options::smlTest( Bu::Array<Bu::String> aArgs )
{
	Bu::String sContent;
	Bu::File fIn( aArgs[1], Bu::File::Read );
	while( !fIn.isEos() )
	{
		char buf[4096];
		sContent.append( buf, fIn.read( buf, 4096 ) );
	}
	fIn.close();

	SmlNode *pRoot = SmlNode::parse( sContent );
//	sio << *pRoot << sio.nl;

	try
	{
		InterfacePlugger &ip = InterfacePlugger::getInstance();
		Interface *pIface = ip.instantiate( sInterface );
		pIface->display( pRoot );
		ip.destroy( pIface );
	}
	catch( Bu::HashException &e )
	{
		sio << "No such interface found." << sio.nl;
	}

	delete pRoot;

	exit( 0 );
	return 0;
}

int Options::smlHelp( Bu::Array<Bu::String> aArgs )
{
	Bu::String sContent = Datafiles::getString("sml-help.sml");
	SmlNode *pRoot = SmlNode::parse( sContent );

	try
	{
		InterfacePlugger &ip = InterfacePlugger::getInstance();
		Interface *pIface = ip.instantiate( sInterface );
		pIface->display( pRoot );
		ip.destroy( pIface );
	}
	catch( Bu::HashException &e )
	{
		sio << "No such interface found." << sio.nl;
	}

	delete pRoot;

	exit( 0 );
	return 0;
}

int Options::nonOption( Bu::Array<Bu::String> aArgs )
{
	sFile = aArgs[0];

	return 0;
}

int Options::printResult( Bu::Array<Bu::String> aArgs )
{
	Gats::GatsStream gsIn( sioRaw );
	Gats::Object *pObj = gsIn.readObject();
	Gats::Dictionary *pDict = dynamic_cast<Gats::Dictionary *>(pObj);

	sio << pDict->getStr("result") << sio.nl << sio.nl << "Running: "
		<< pDict->getBool("running") << sio.nl;

	delete pObj;

	exit( 0 );

	return 0;
}